<template>
  <BasicDrawer v-bind="$attrs" @register="registerDrawer" showFooter title="用户信息" :width="600" @ok="handleSubmit" destroyOnClose>
    <BasicForm @register="registerForm"></BasicForm>
  </BasicDrawer>
</template>
<script lang="ts" setup>
import { BasicDrawer, useDrawerInner } from '@apps/antd-admin/src/components/drawer';
import { BasicForm, useForm } from '@apps/antd-admin/src/components/form';
import { sendMessageFormSchema } from '../data/UserGoodsData';
import { ref, unref } from 'vue';
import { sendMessage } from '../data/UserGoodsApi';

const emit = defineEmits(['success', 'register']);
const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
  labelWidth: 120,
  labelCol: { span: 5 },
  schemas: sendMessageFormSchema,
  showActionButtonGroup: false,
});
const isUpdate = ref(true);
const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
  resetFields();
  data.value = !!data?.isUpdate;

  if (unref(isUpdate)) {
    let record = data.record;
    setFieldsValue({
      ...record,
    });
  }
});

async function handleSubmit() {
  try {
    const values = await validate();
    console.info(values);
    setDrawerProps({ confirmLoading: true });
    if (unref(isUpdate)) {
      await sendMessage(values);
    }
    closeDrawer();
    emit('success', false);
  } finally {
    setDrawerProps({ confirmLoading: false });
  }
}
</script>
